package com.cakebackoffice.Model;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakebackoffice.database.DataSource;
import com.cakebackoffice.database.DeliveryTypeTable;
import com.cakebackoffice.database.IDatabasable;

import java.util.ArrayList;
import java.util.List;

public class DeliveryType implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private String name;
    private String description;

    private DeliveryType(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(DeliveryTypeTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(DeliveryTypeTable.REMOTE_ID)));
        name = cursor.getString(cursor.getColumnIndexOrThrow(DeliveryTypeTable.NAME));
        description = cursor.getString(cursor.getColumnIndexOrThrow(DeliveryTypeTable.DESCRIPTION));
    }

    public DeliveryType(com.cakebackoffice.clientsoap.contract.DeliveryType deliveryType) {
        this.remoteID = Long.valueOf(deliveryType.id);
        this.name = deliveryType.name;
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(DeliveryTypeTable.REMOTE_ID, this.getRemoteID());
        values.put(DeliveryTypeTable.NAME, this.name);
        values.put(DeliveryTypeTable.DESCRIPTION, this.description);
        return values;
    }

    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        DeliveryType bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = DeliveryType.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, DeliveryTypeTable.getInstance());
        } else {
            dataSource.update(this, DeliveryTypeTable.getInstance());
        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, DeliveryTypeTable.getInstance());
            dataSource.close();
        }
    }

    public static DeliveryType getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, DeliveryTypeTable.getInstance());
        cursor.moveToFirst();
        DeliveryType item = null;
        while (!cursor.isAfterLast()) {
            item = new DeliveryType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static DeliveryType getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, DeliveryTypeTable.getInstance());
        cursor.moveToFirst();
        DeliveryType item = null;
        while (!cursor.isAfterLast()) {
            item = new DeliveryType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<DeliveryType> getAll(Context context) {
        List<DeliveryType> result = new ArrayList<DeliveryType>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(DeliveryTypeTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new DeliveryType(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static DeliveryType getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, DeliveryTypeTable.getInstance());
        cursor.moveToFirst();
        DeliveryType item = null;
        while (!cursor.isAfterLast()) {
            item = new DeliveryType(dataSource, cursor);
            cursor.moveToNext();
        }
        return item;
    }

    @Override
    public String toString() {
        return name;
    }
}
